package com.attms.service.impl;

import com.attms.controller.utils.R;
import com.attms.dao.AbsentDao;
import com.attms.dao.EmployeeDao;
import com.attms.domain.Absent;
import com.attms.domain.Employee;
import com.attms.service.IEmployeeService;
import com.attms.service.IPersonnelService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.Year;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PersonnelServiceImpl extends ServiceImpl<EmployeeDao, Employee> implements IPersonnelService {
    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private AbsentDao absentDao;

    public List<Employee> getAllInfo(){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID","E_NAME","E_GENDER","E_BIRTH","E_START","E_JOB", "E_DEPARTMENT");
        return employeeDao.selectList(wrapper);
    }

    @Override
    public List<Employee> getAllReport() {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID","E_NAME","E_GENDER", "E_DEPARTMENT","E_ABSHOUR");
        return employeeDao.selectList(wrapper);
    }

    public Employee getInfoById(Integer EmployeeId){
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.select("E_ID","E_NAME","E_GENDER","E_BIRTH","E_START","E_JOB", "E_DEPARTMENT");
        return employeeDao.selectById(EmployeeId);
    }

    public boolean deleteById(Integer EmployeeId){
        return employeeDao.deleteById(EmployeeId) > 0;
    }

    public boolean modifyById(Employee employee){
        return employeeDao.updateById(employee) > 0;
    }

    public boolean add(Employee employee){
//        添加员工把应有的假期算好
//        传进来的参数有：、姓名、性别、出生日期、入职日期、职位、密码、部门
//        自动算的参数有6种假期的剩余天数、扣除工资小时数为0、工号
//        暂时为空的参数有：出勤状态、签到签退时间
        int gender = employee.getE_GENDER();
        int workTime = employee.getE_START().until(LocalDate.now()).getDays();
        int age = employee.getE_BIRTH().until(LocalDate.now()).getYears();
        employee.setE_STATE(1);//默认为缺勤状态
        employee.setE_ABSHOUR(0);
//        年假
        if(workTime < 1){
            employee.setE_RYEAR(0);
        }else if(workTime< 10){
            employee.setE_RYEAR(5);
        }else if(workTime < 20){
            employee.setE_RYEAR(10);
        }else{
            employee.setE_RYEAR(15);
        }
//        病假5天
        employee.setE_RILL(5);
//        婚假
        if((gender==0 && age <= 23)||(gender==1 && age<=25)){
            employee.setE_RWEDDING(3);
        }else{
            employee.setE_RWEDDING(10);
        }
//        产假、陪产假
        if(gender==0){
            employee.setE_RPATERNITY(0);
            employee.setE_RMATERNITY(98);
            employee.setE_RLACTATION(60);//60min
        }else{
            employee.setE_RPATERNITY(7);
            employee.setE_RMATERNITY(0);
        }
//
        return employeeDao.insert(employee) > 0;
    }

    @Override
    public boolean solveLeave(Integer isAgree, Absent absenter) {
        LambdaQueryWrapper<Absent> Awrapper = new LambdaQueryWrapper<>();
        Awrapper.eq(Absent::getA_ID,absenter.getA_ID());
        Absent absence = absentDao.selectOne(Awrapper);

        if(isAgree==0){
            absence.setA_RESPON(absenter.getA_REASON());
            absence.setA_STATE(4);
        }else{
            absence.setA_RESPON("人事审核通过");
            absence .setA_STATE(3);
            int days = absence.getA_ENDTIME().until(absence.getA_STTIME()).getDays();
            //如果是因公请假，则不扣薪，只记录
            Employee nowperson = employeeDao.selectById(absence.getE_ID());
            if(absence.getA_TYPE()==8){//如果事假，则记录扣薪时长
                int beforeAbs = nowperson.getE_ABSHOUR();
                nowperson.setE_ABSHOUR(beforeAbs + days * 8);
                employeeDao.updateById(nowperson);
            }else if(absence.getA_TYPE() > 0){//如果是带薪假期，则只修改请的假的剩余天数
                int vocationType = absence.getA_TYPE();
                if(vocationType == 1)nowperson.setE_RYEAR(nowperson.getE_RYEAR() - days);
                else if(vocationType == 2)nowperson.setE_RILL(nowperson.getE_RILL() - days);
                else if(vocationType == 3)nowperson.setE_RWEDDING(nowperson.getE_RWEDDING() - days);
                else if(vocationType == 5)nowperson.setE_RMATERNITY(nowperson.getE_RMATERNITY() - days);
                else if(vocationType == 6)nowperson.setE_RLACTATION(nowperson.getE_RLACTATION() - (int)Duration.between(absence.getA_STTIME(), absence.getA_ENDTIME()).toHours());
                else if(vocationType == 7)nowperson.setE_RPATERNITY(nowperson.getE_RPATERNITY() - days);
                employeeDao.updateById(nowperson);
            }
        }
        absentDao.updateById(absence);
        return true;
    }


}
